package com.region.http.client.resolver.parameter;

import com.region.common.utils.RandomUtils;
import com.region.http.client.model.MultiFileTransferMeta;

import java.io.File;
import java.io.InputStream;

/**
 * @author liujieyu
 * @date 2023/8/27 20:49
 * @description
 */
public class MultipartParameterResolver extends AbstractParameterResolver {

    @Override
    public int getPriority() {
        return 0;
    }

    @Override
    public Object resolver(Object arg) throws Throwable {
        if (arg == null) return null;
        if (arg instanceof File[]) {
            File[] files = (File[]) arg;
            MultiFileTransferMeta.MultiFileTransferMetaBuilder builder = MultiFileTransferMeta.builder();
            for (int i = 0; i < files.length; i++) {
                builder.addFile(files[i]);
            }
            return builder.build();
        } else if (arg instanceof InputStream[]) {
            InputStream[] inputStreams = (InputStream[]) arg;
            MultiFileTransferMeta.MultiFileTransferMetaBuilder builder = MultiFileTransferMeta.builder();
            for (int i = 0; i < inputStreams.length; i++) {
                builder.addInputStream(RandomUtils.getId(), inputStreams[i]);
            }
            return builder.build();
        }
        return arg;
    }
}
